package com.online.player.client;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.online.player.client.shared.MenuItem;

public class Menu {

	private Tree menuTree;
	private Map<MenuItem, TreeItem> items = new HashMap<MenuItem, TreeItem>();
	private Map<TreeItem, IPage> pages = new HashMap<TreeItem, IPage>();
	private IPage empty = new IPage() {
		
		public void show() {}
		
		public void hide() {}
	}; 
	private IPage currentPage = empty;

	public void create() {
		RootPanel root = RootPanel.get("gwt-menu");

		menuTree = new Tree();

		TreeItem i1 = createItem("Root1");
		items.put(MenuItem.PLAYER, i1);
		i1.addItem(createItem("Child1"));
		i1.addItem(createItem("Child2"));

		TreeItem i2 = createItem("Root2");
		items.put(MenuItem.MANAGE_VIDEO, i2);

		menuTree.addItem(i1);
		menuTree.addItem(i2);

		root.add(menuTree);

		menuTree.addSelectionHandler(new SelectionHandler<TreeItem>() {

			public void onSelection(SelectionEvent<TreeItem> event) {
				IPage page = pages.get(event.getSelectedItem());
				if(page == null)
					page = empty;
				currentPage.hide();
				page.show();
				currentPage = page;
			}
		});
	}

	public void setPage(MenuItem menuItem, IPage page) {
		pages.put(items.get(menuItem), page);
	}

	private TreeItem createItem(String name) {
		TreeItem item = new TreeItem();
		item.setText(name);
		return item;
	}
}
